﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using AbstraX.Contracts;
using System.ServiceModel.Activation;
using System.Threading;
using AbstraX;
using System.Diagnostics;

namespace SolutionFramework.Web
{
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class BuildService : IBuildService
    {
        public void BuildMiddleLayer(string id)
        {
            Global.DomainServiceHostApplication.BuildUIQueue.Clear();
            Global.DomainServiceHostApplication.PipelineService.GenerateMiddleLayer(id);
        }

        public bool MoveNext(string lastUIComponentURL)
        {
            var daemon = Global.DomainServiceHostApplication.BuildUIQueue.Where(e => e.URL == lastUIComponentURL).Select(e => e.BuildUIDaemon).Single();
            var retval = daemon.MoveNext(lastUIComponentURL);

            Debug.Assert(daemon == Global.DomainServiceHostApplication.BuildUIQueue.Peek().BuildUIDaemon);

            Global.DomainServiceHostApplication.BuildUIQueue.Dequeue();

            Global.DomainServiceHostApplication.ClearWait();

            return retval;
        }

        public void AbortBuild(string lastUIComponentURL)
        {
            var daemon = Global.DomainServiceHostApplication.BuildUIQueue.Where(e => e.URL == lastUIComponentURL).Select(e => e.BuildUIDaemon).Single();
            var retval = daemon.MoveNext(lastUIComponentURL);

            Debug.Assert(daemon == Global.DomainServiceHostApplication.BuildUIQueue.Peek().BuildUIDaemon);

            daemon.Abort();

            Global.DomainServiceHostApplication.BuildUIQueue.Dequeue();

            Global.DomainServiceHostApplication.ClearWait();
        }

        public string GetProperty(string UIComponentURL, string propertyName)
        {
            var daemon = (IBuildUIDaemon)Global.DomainServiceHostApplication.BuildUIQueue.Where(e => e.URL == UIComponentURL).Select(e => e.BuildUIDaemon).Single();
            return daemon.GetProperty(UIComponentURL, propertyName);
        }

        public void SetProperty(string UIComponentURL, string propertyName, string value)
        {
            try
            {
                var daemon = (IBuildUIDaemon)Global.DomainServiceHostApplication.BuildUIQueue.Where(e => e.URL == UIComponentURL).Select(e => e.BuildUIDaemon).Single();
                daemon.SetProperty(UIComponentURL, propertyName, value);
            }
            catch
            {
                throw;
            }
        }

        public void Finish(string lastUIComponentURL)
        {
            var daemon = (IBuildUIDaemon)Global.DomainServiceHostApplication.BuildUIQueue.Where(e => e.URL == lastUIComponentURL).Select(e => e.BuildUIDaemon).Single();
            daemon.Finish(lastUIComponentURL);

            Global.DomainServiceHostApplication.ClearWait(true);
        }
    }
}
